
// 基类：形状
class Shap {

    constructor ()
    {
        console.log('子类必须实现')
    };

    draw ()
    {
        console.log('子类必须实现')
    };

    area ()
    {
        console.log('子类必须实现')
    };
};

// 圆形
class Circle extends Shap {

    constructor (Radius)
    {
        super();

        this.Radius = Radius;
    };

    draw ()
    {
        console.log('这是一个圆，半径为' + this.Radius);
    };

    area ()
    {
        let mj = (Math.PI * this.Radius * this.Radius).toFixed(2)
        console.log('面积为：' + mj);
    };
};

// 多边形
class Polygon extends Shap {

    constructor (width, height)
    {
        super();

        this.width = width;
        this.height = height;
    };

    draw ()
    {
        console.log('这是一个多边形，长为' + this.width + ',宽为' + this.height);
    };

    area ()
    {
        let mj = this.width * this.height;
        console.log('面积为：' + mj);
    };
};

// 矩形
class Rectangle extends Polygon {

    constructor (width, height)
    {
        super(width, height);
    };

    draw ()
    {
        console.log('这是一个矩形，长为' + this.width + ',宽为' + this.height);
    };

    area ()
    {
        super.area();
    };
};

// 三角形
class Triangle extends Polygon {

    constructor (width, height)
    {
        super(width, height);
    };

    draw ()
    {
        console.log('这是一个三角形，底为' + this.width + ',高为' + this.height);
    };

    area ()
    {
        let mj = this.width * this.height / 2;
        console.log('面积为：' + mj);
    };
};

//创建一个圆，参数2为半径
let circle = new Circle(2);   

//创建一个矩形，参数2为宽，参数4为高
let rect = new Rectangle(2, 4);

//创建一个三角型，参数3为底，参数6为高
let triangle = new Triangle(3, 6);

//将图形放入数组，统一执行 draw 与 are 方法
let array = [circle, rect, triangle];
array.forEach(shap => {
    shap.draw();  //例如当前shap是一个圆，输出：画一个圆，半径为2   
    shap.area();  //例如当前shap是一个圆，输出：12.57(保留两位小数)
});

//------------分割线-------------------------

function task(array, callback, target)
{
    let length = array.length, index = 0;
    let test = setInterval( ()=>
    {
        if(index === length)
        {
            callback.call(target);
            clearInterval(test);
        }

        console.log(array[index]);
        index++;
    }, 100)
};

let target = {};
let newArr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
task(newArr, function(){
    console.log(this === target);
}, target)
